<?php

class RankAction extends Action {
	public function index() {

	}

	public function checkGrades() {
		$Dao = M('homeworkinfo');
		$condition['id'] = $_GET['hw_id'];
		$homeworks = $Dao -> where($condition) -> select();
		$homework = $homeworks[0];
		$this -> hw_head = $homework['head'];
		$this -> hw_content = $homework['content'];
		$this -> hw_create_time = date('Y-m-d', $homework['create_time']);
		$this -> hw_deadline = date('Y-m-d', $homework['deadline']);
        $this -> hw_id = $homework['id'];
		if (isset($_SESSION['id'])) {
			$this -> id = $_SESSION['id'];
			$this -> name = $_SESSION['name'];
		}

		$Dao1 = M('stuhomework');
		$condition2['hw_id'] = $_GET['hw_id'];
		$stuhws = $Dao1 -> where($condition2) -> select();
		$studentsid;
		$studentsname;
		$groups;
		foreach ($stuhws as $key => $value) {
			
			$stu_id = $value['stu_id'];
			 
			$student_dao = M('student');
			$condition3['id'] = $stu_id;
			$result = $student_dao->where($condition3)->find();
			
			$condition4['id'] = $result['group_id'];
			$group_dao = M('group');
			$result2 = $group_dao->where($condition4)->find();
			
			$stuhws[$key]['studentid'] = $result['studentid'];
			$stuhws[$key]['studentname'] = $result['name'];
			$stuhws[$key]['group_name'] = $result2['name'];
			
			$studentsid[] = $result['studentid'];
			$studentsname[] = $result['name'];
			$groups[] = $result2['name']; 
			
		}
		
		for($i=0; $i<count($stuhws) ;$i++){
			for($j=0; $j<$i; $j++){
				if($stuhws[$i]['group_name'] > $stuhws[$j]['group_name']){
				$temp = $stuhws[$i];
				$stuhws[$i] = $stuhws[$j];
				$stuhws[$j] = $stuhws;
				}
			}
        }
		
		$this -> assign('stuhws', $stuhws);
		// print_r($studentsid);
		// print_r($studentsname);
		// print_r($groups);
		
        $this ->assign('students', $result);
		$this->assign('studentsid', $studentsid);
		$this->assign('studentsname', $studentsname);
		$this->assign('groups',$groups);
		$this -> display();
	}

	public function allGradesAdmin() {
		$hw_id = $_GET['hw_id'];
		$stuhomework_dao = M('stuhomework');
		$condition;
		$condition['hw_id'] = $hw_id;
		$result = $stuhomework_dao->where($condition)->select();
		
		$studentsid;
		$studentsname;
		foreach ($result as $key => $value) {
			$stu_id = $value['stu_id'];
			$student_dao = M('student');
			$condition2['id'] = $stu_id;
			$result2 = $student_dao->where($condition2)->find();
			
			$result[$key]['studentid'] = $result2['studentid'];
			$result[$key]['name'] = $result2['name'];
			$studentsid[] = $result2['studentid'];
			$studentsname[] = $result2['name'];
		}
		
		$id = $_SESSION['admin_id'];
		echo $id;
		$condition3['id'] = $id;
		$admin_dao = M('admin');
		$admin_result = $admin_dao->where($condition3)->find();
		$this->assign("admin", $admin_result);
		$this->assign("students", $result);
		//print_r($studentsid);
		//print_r($admin_result);
		$this->display();
		
	}
	public function rank(){
		$grades = $_POST['grades'];
		foreach ($grades as $key => $value) {
			$stu_id = $_POST['studentsid'][$key];
			$hw_id = $_POST['hwids'][$key];
			$point = $_POST['grades'][$key];
			
			$condition['stu_id'] = $stu_id;
			$condition['hw_id'] = $hw_id;
			$data['point'] = $point;
			
			$stuhomeworkinfo_dao = M('stuhomework');
			$stuhomeworkinfo_dao->where($condition)->save($data);
			$this->redirect("Index/adminMain");
		}
	}

}
?>